document.addEventListener('DOMContentLoaded',init,false);
var conn;//global connection object
function init() {
    wireUpCommands(["login","sql-eval","list-tables"]);
}
function login_handler(res) {
    var xmlist = res.responseXML;
    //clear errors
    var el=$('login-container')
    el.className=el.className.replace('error','');
    var dblist=$('databases');
    if(xmlist.documentElement.tagName!='result') {
        //handle the error ourselves
        login_error_handler(res);//pass the response
    }
    var databases=xmlist.evaluate('//value',xmlist,null,XPathResult.ANY_TYPE,null);
    var database;
    //find number of result nodes
    while(database=databases.iterateNext()) {
        var nd=document.createElement('listitem');
        nd.setAttribute("label",database.textContent);
        dblist.appendChild(nd);
    }
}
window["show-tables_handler"]=function(res) {
    var el=$('tables');
    while(el.hasChildNodes()) el.removeChild(el.firstChild);
    var xmlist=res.responseXML;
    if(xmlist.documentElement.tagName!='result') {
        throw new Error("invalid response!");
    }
    var tables=xmlist.evaluate('//value',xmlist,null,XPathResult.ANY_TYPE,null);
    var table;
    //find number of result nodes
    while(table=tables.iterateNext()) {
        var nd=document.createElement('listitem');
        nd.setAttribute("label",table.textContent);
        el.appendChild(nd);
    }
}
function login_error_handler(res) {
    //assume that an error happened
    $('login-container').className="error";
}
function do_login() {
    var password,login,remoteUrl;
    var password=$('password').value;
    var login=$('user-name').value;
    var remoteUrl=$('host-name').value;
    conn = db.connect(
	{
	    user:login,
	    password:password,
	},
	remoteUrl,//*should be https only
	{
	    success:login_handler,
	    failure:function(e) {dump('response was not valid xml\n'+e.responseText);}
	}
    );//specifically assiging to new variable

}
window['do_sql-eval'] = function() {
    if( conn == undefined ) {
          alert("Please make a connection!");
          $('main').selectedIndex=0;
    }
    var sql=$('sql-shell').value;
    conn.cursor().execute(sql,null,window["do_sql-eval_handler"]);
    $('sql-exec-results').className="success";
}//set as property of window becasue variables can't have - in their name
window['do_sql-eval_handler']=function(res) {
    var el=$('sql-exec-results');//now clear elements
    while(el.hasChildNodes()) el.removeChild(el.firstChild);
    xml=res.responseXML;
    if(xml.documentElement.tagName!='result') {throw new Error("invalid result!")}
    //fill in row names
    var row_names=xml.documentElement.childNodes[0].childNodes;
    var row = document.createElement('listhead');
    for(var i=0;i<row_names.length;i++) {
        var list_header=document.createElement('listheader');
        list_header.setAttribute('label',row_names[i].getAttribute('name'));
        row.appendChild(list_header);
    }
    el.appendChild(row);
    var rows=xml.evaluate('//row',xml,null,XPathResult.ANY_TYPE,null);
    var row=null;
    //find number of result nodes
    while(row=rows.iterateNext()) {
        var nd=document.createElement('listitem');
        for(var i=0;i<row.childNodes.length;i++) {
            cell_data=row.childNodes[i];
            cell=document.createElement('listcell');
            cell.setAttribute('label',cell_data.textContent);
            nd.appendChild(cell);
        }
        el.appendChild(nd);
    }
}
window['do_list-tables']=function() {
    
    cur=conn.cursor();
    conn._conn_info.db=$('databases').currentItem.getAttribute("label");//cache a reference to the current database
    cur.execute("SHOW TABLES",null,window["show-tables_handler"]);
};
function $(id) {
    return document.getElementById(id);
}
function wireUpCommands(names) {
    for(var i=0;i<names.length;i++) {
        $( names[i] ).addEventListener( 'command' , window[ "do_" + names[ i ] ] ,false );
    }
}
